package com.smzd.projforge.business;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smzd.core.business.AbstractService;
import com.smzd.core.dao.IDao;
import com.smzd.core.utils.db.RPage;
import com.smzd.core.utils.db.SRPage;
import com.smzd.core.utils.json.FGRowConverter;
import com.smzd.projforge.dao.DynaClassDao;
import com.smzd.projforge.dao.MTMRelationDao;
import com.smzd.projforge.dao.ProjectDao;
import com.smzd.projforge.domain.DynaClass;
import com.smzd.projforge.domain.MTMRelation;
import com.smzd.projforge.domain.Project;
import com.smzd.projforge.domain.Suit;

@Service
public class DynaClassService extends AbstractService<DynaClass> {
	@Autowired
	private DynaClassDao dao;
	@Autowired
	private MTMRelationDao mmDao;
	
	@Autowired
	private ProjectDao projDao;

	@Override
	protected IDao<DynaClass> getDao() {
		return dao;
	}

	@Override
	protected FGRowConverter<DynaClass> getConverter() {
		return ConverterHolder.converter;
	}

	static class ConverterHolder {
		static final FGRowConverter<DynaClass> converter = new FGRowConverter<DynaClass>() {
			@Override
			public Object[] convert(DynaClass elem) {
			    Suit suit = elem.getSuit();
				Object[] fs = new Object[] { 
						elem.getId(), 
						elem.getName(),
						elem.getClassName(),
						elem.getEntryName(),
						elem.getEntryNames(),
						elem.getTableName(),
						elem.getTableAlias(),
						elem.getOwnerLevel(),
						AbstractService.getObjectByField(elem.getScopeClass()),
						elem.getDescription(),
						AbstractService.getObjectByField(suit)
				};
				return fs;
			}
		};
		static final FGRowConverter<DynaClass> checkedConverter = new FGRowConverter<DynaClass>() {
			@Override
			public Object[] convert(DynaClass elem) {
				Object[] fs = new Object[] { 
						elem.getId(), 
						elem.getName(),
						elem.getClassName(),
						elem.getOwnerLevel(),
						elem.getDescription(),
						elem.getChecked()
				};
				return fs;
			}
		};
	}

	public String getMasterClassesData(RPage<DynaClass> page) {
		dao.getMasterClassListByPage(page);
		return page.toFlexiGridJsonString(ConverterHolder.checkedConverter);
	}

	public void saveMMRelations(int scopeId, int slaveId, String masterIds) {
		String[] ids = masterIds.split(",");
		List<MTMRelation> toAdd = new ArrayList<>();
		List<MTMRelation> toDel = new ArrayList<>();
		for(String id : ids){
			int masterId = Integer.parseInt(id);
			MTMRelation mm = new MTMRelation();
			mm.setSlave(new DynaClass(slaveId));
			if(masterId<0){
				mm.setMaster(new DynaClass(-masterId));
				toDel.add(mm);
			}else{
				mm.setName("Auto");
				mm.setDescription("Auto");
				mm.setScope(new Project(scopeId));
				mm.setMaster(new DynaClass(masterId));
				toAdd.add(mm);
			}
		}
		mmDao.saveMTMRelations(toAdd, toDel);
	}

	public String getMTMRelationClassCandidates(int scopeId) {
		SRPage<Project, DynaClass> page = new SRPage<>();
		page.setScope(new Project(scopeId));
		page.setPageNo(1);
		page.setPageSize(999);
		dao.getMTMRelationClassCandidates(page);
		return page.toFlexiGridJsonString(ConverterHolder.checkedConverter);
	}
}
